<%@ page language="java" pageEncoding="utf8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US"
	dir="ltr">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link rel="stylesheet" href="<c:url value="/css/shopadmin.css"/>"
			type="text/css" />
		<link rel="stylesheet"
			href="<c:url value='/scripts/themes/base/jquery.ui.all.css'/>"
			type="text/css" />
	</head>
	<body style="">
		<div id="headBar" class="head_content"></div>
		<div id="main" class="main_content">
			<form   action="mt_activity_rule_save.html"  onsubmit="return checkLevel();" method="post" id="mt_activity_rule_form">
			<input type="hidden"   name="marketPromotion.pmtId"  value="${marketPromotion.pmtId}" >
			<input type="hidden"   name="marketPromotion.relatedId"  value="${marketPromotion.relatedId}"/>
			<input type="hidden"    name="marketPromotion.relatedType"  value="activity"/>
			<input type="hidden"    name="marketPromotion.pmtsId"  value="${marketPromotionScheme.pmtsId }"/>
			<input type="hidden"    name="ruleChanged"  value="${ruleChanged}"/>
				<div class="tableform">
					<h4>
						优惠方式
					</h4>
					<div class="division">
						<table>
							<tbody>
								<tr>
									<th>
										订单优惠条件：
									</th>
									<td>
										<input type="text" size="10" value="${marketPromotion.orderMoneyFrom}"   name="marketPromotion.orderMoneyFrom" class="x-input " >
											<b>≤</b>订单金额 ＜ 
										<c:if test="${not empty marketPromotion.orderMoneyTo}">
										<input type="text"   name="marketPromotion.orderMoneyTo" value="${marketPromotion.orderMoneyTo}"  size="10" class="x-input "  > 
										</c:if>
										<c:if test="${empty marketPromotion.orderMoneyTo}">
										<input type="text"   name="marketPromotion.orderMoneyTo" value="999999"  size="10" class="x-input "  > 
										</c:if>
									</td>
								</tr>
								<tr>
									<th>
										允许参加会员等级：
									</th>
									<td id="promotion_level">
										<c:forEach var="level" items="${marketPromotionMemberLevels}"  varStatus="index">
										<input type="hidden"   name="marketPromotionMemberLevels[${index.index }].pmlId"  value="${level.pmlId}"/>										
										<input type="checkbox" value="${level.memberLvId}"   vname="promotion_level"
											<c:if test="${not empty level.pmlId }">checked="checked"</c:if> 										
											name="marketPromotionMemberLevels[${index.index }].memberLvId"/>
										 ${level.levelName}
										</c:forEach>
										
									</td>
								</tr>
							</tbody>
						</table>
					</div>
				</div>
				<div class="tableform">
					<h4>
						其他设置
					</h4>
					<div class="division">
						<table width="100%" cellspacing="0" cellpadding="0" border="0">
							<tbody>
								<tr>
									<th>
										活动期间是否允许使用优惠券:
									</th>
									<td>
										<select id="marketPromotion_pmtIfcoupon" name="marketPromotion.pmtIfcoupon" style="width:60px;">
											<option value="true">是</option>
											<option value="false">否</option>
										</select>
									</td>
								</tr>
								<tr>
									<th>
										活动规则开始时间:
									</th>
									<td>

										<input size="10" id="rule_start_time"   name="marketPromotion.pmtTimeBeginString"  value="${marketPromotion.pmtTimeBeginString}"  class="cal cal"   type="text">

									</td>
								</tr>
								<tr>
									<th>
										活动规则结束时间:
									</th>
									<td>

										<input size="10" id="rule_end_time"   name="marketPromotion.pmtTimeEndString"  value="${marketPromotion.pmtTimeEndString}"  class="cal cal"   type="text">

									</td>
								</tr>
							</tbody>
						</table>
					</div>
				</div>
				<div class="tableform">
					<h4>
						优惠内容
					</h4>
					<input type="hidden"    name="marketPromotion.pmtBasicType"  value="order"/>
									<input type="hidden"    name="marketPromotion.pmtMethod"  value="give_coupon"/>
									<input type="hidden"    name="marketPromotion.pmtMethodValue"  value=" "/>
									<input type="hidden"    name="marketPromotion.pmtBondType"  value="2"/>
					<hr class="clear"/>
						<div class="division">
							<h5>
								送优惠券
							</h5>
							<div class="clearfix" id="ipt_el-ff003-4883e">
								<div class="object-select clearfix">
									<div class="label"  onclick="openProductDialog()">
										请选择...
									</div>
									<div class="handle"  onclick="openProductDialog()">
										&nbsp;
									</div>
									
								</div>
								<div class="gridlist rows-body" id="pp_row_box">
								</div>
							</div>
						</div>
				</div>
				<div class="tableform">
					<h4>
						规则描述
					</h4>
					<textarea required="true" rows="6" cols="60" name="marketPromotion.pmtDescribe">${marketPromotion.pmtDescribe }</textarea>
					<br>
						<span class="fontcolorGray">此描述会显示在前台相关页面展示给顾客 </span>
				</div>
				<div class="table-action">
					<button  onclick="location.href='mt_activity_rule_begin.html?marketPromotionActivity.pmtaId=${marketPromotionActivity.pmtaId}&pmtId=${marketPromotion.pmtId}&pmtsId=${oldPtmsId}'"
					class="btn" type="button">
						<span><span>上一步</span> </span>
					</button>
					<button class="btn" id="x-tonext-btn" type="submit">
						<span><span>下一步</span> </span>
					</button>
					<button class="btn" type="button">
						<span><span>取消</span> </span>
					</button>
				</div>
			</form>
		</div>
		<div id="footBar" class="foot_content"></div>
		
		
		<div id="gift_dialog" title="优惠券列表">
			
			<div id="search-result" >
				<div class="gridlist-head">请选择 </div>
				<div class="gridlist selectorlist" id="selector-body">


				</div>
				<div class="dialog-content-foot">
					<div class="table-action">
						<button class="btn"
							onclick="addRow();$('#gift_dialog').dialog('close');"
							type="button">
							<span><span>确定</span> </span>
						</button>
						<button class="btn"
							onclick="$('#gift_dialog').dialog('close');"
							type="button">
							<span><span>取消</span> </span>
						</button>
					</div>
				</div>
			</div>
		</div>
		<div id="goods-result-row-tmpl" style="display: none;">
			<div class="row">
				<div class="row-line">
					<input name="selected_product" vname="#name#" value="#couponId#"  type="checkbox" />
					<label >
						#name#
					</label>
				</div>
			</div>
		</div>
		<div id="goods-row-tmpl" style="display: none;">
			<div class="row">
				<div class="row-line">
					<div class="span-1">
						<span class="opt"><img
								style="width: 16px; height: 16px; background-position: 0pt -214px;"
								class="imgbundle" onclick="deleteRow(this,#_rgcId#)"
								src="<c:url value="/images/transparent.gif"/>" /> </span>
					</div>
					<div class="span-auto  " style="width: 350px;">
						<input type="hidden" value="#couponId#"		name="marketPromotionGenCoupons[#index#].cpnsId" />
						<input type="hidden" value="#rgcId#"	name="marketPromotionGenCoupons[#index#].rgcId" />
						#name#
					</div>
				</div>
			</div>
		</div>
		
		
		
		
		
		<script type="text/javascript"
			src="<c:url value='/scripts/jquery-1.4.2.js'/>"></script>
		<script type="text/javascript"
			src="<c:url value='/scripts/jquery.validate.js'/>"></script>
		
 
		<script type="text/javascript"
			src="<c:url value='/scripts/jquery.bgiframe-2.1.1.js'/>"></script>
		<script type="text/javascript"
			src="<c:url value='/scripts/ui/jquery.ui.core.js'/>"></script>
		<script type="text/javascript"
			src="<c:url value='/scripts/ui/jquery.ui.widget.js'/>"></script>
		<script type="text/javascript"
			src="<c:url value='/scripts/ui/jquery.ui.mouse.js'/> "></script>
		<script type="text/javascript"
			src="<c:url value='/scripts/ui/jquery.ui.button.js'/>"></script>
		<script type="text/javascript"
			src="<c:url value='/scripts/ui/jquery.ui.draggable.js'/>"></script>
		<script type="text/javascript"
			src="<c:url value='/scripts/ui/jquery.ui.position.js'/>"></script>
		<script type="text/javascript"
			src="<c:url value='/scripts/ui/jquery.ui.dialog.js'/>"></script>
		<script type="text/javascript"
			src="<c:url value='/scripts/ui/jquery.effects.core.js'/> "></script>
		<script type="text/javascript"
			src="<c:url value='/scripts/ui/jquery.effects.drop.js'/>"></script>
		<script type="text/javascript" src="<c:url value='/scripts/ui/jquery.ui.datepicker.js'/>"></script>
		<script type="text/javascript" src="<c:url value='/scripts/ui/i18n/jquery.ui.datepicker-zh-CN.js'/>"></script>
		
		<script type="text/javascript">
		
		<c:if test="${not empty marketPromotion.pmtId}">
			$('#marketPromotion_pmtIfcoupon').attr('value','${marketPromotion.pmtIfcoupon}');		
		</c:if>
		
		
		
		String.prototype.replaceAll = function(s1, s2) {
			return this.replace(new RegExp(s1, "gm"), s2);
		}
	
	
	
	function searchSubmit(page_id){
			var url = "mt_coupon_json_list.html";			
			var params = {
				"pagerId"  		:	page_id
			};
			$.post(url, params, 
				function callback(data) 
				{	
					var coupons = data.marketCouponss;
					$('#selector-body').html('');
					$.each( coupons, function(i, coupon){  				
					
		  				var tmpl = $('#goods-result-row-tmpl').html();
						tmpl = tmpl.replaceAll('#name#', coupon.couponName);	
						tmpl = tmpl.replaceAll('#couponId#', coupon.couponId);
						$('#selector-body').append(tmpl);
  							
  					});
  					var pagerStr = data.pagerString;
  					$('<div class="gridlist-footer" >'+pagerStr +'</div>').appendTo('#selector-body');
				},
				"json"
			);
			
			 
			
		
		}
		
		function deleteRow(clickObj,rgcId){
			if(!confirm('<fmt:message key="common.confirm_deleted" />')){
				return;
			}
			if(rgcId!=0){
				deletePromotionGoupon(rgcId);
			}
			$(clickObj).parent().parent().parent().remove(); 
		}
		
		function deletePromotionGoupon(rgcId){
			var url = "mt_promotion_coupon_delete.html";
			var params = {
				"rgcId"  		:	rgcId

			};

			$.getJSON(url, params, 
				function callback(data) 
				{	
					alert("删除成功");
				}
			);	
		}
		
		
		 
		 		
		
		function addRow(){
			var existedRate = $("#pp_row_box >div >div >div > input:hidden");			
			$("input[name='selected_product']").each(function(index, domEle) {
				if($(domEle).attr("checked")){
					var bool =true;
					existedRate.each(function(ind, dome){
						if($(domEle).val()==$(dome).val()){
							bool = false;
							return;
						}					
					});
					if(bool){
						
						 addTmplRow($(domEle).attr("vname"),$(domEle).val(),0);
						
  						 	
					}
					
				} 
			});
			
			 
			$('#selector-body').html('');
			
		}
		
		
		<c:forEach var="genCoupon" items="${marketPromotionGenCoupons}"	>
			addTmplRow( '${genCoupon.couponName}' ,${genCoupon.cpnsId},${genCoupon.rgcId});
		</c:forEach>
		var pp_index= 0 ;
		function addTmplRow(name,couponId,rgcId){
				var _rgcId =rgcId;
				if(rgcId==0){
					//在服务器端保证不存在marketPromotionGoods.pgId为0的数据
					rgcId = '';					
				}
				var tmpl = $('#goods-row-tmpl').html();
				tmpl = tmpl.replaceAll('#name#', name);	
				tmpl = tmpl.replaceAll('#couponId#', couponId);
				tmpl = tmpl.replaceAll('#rgcId#', rgcId);
				tmpl = tmpl.replaceAll('#index#', pp_index);
				tmpl = tmpl.replaceAll('#_rgcId#', _rgcId);
			
				pp_index++;	
				$('#pp_row_box').append(tmpl);
		}
		
		 
		
		
		
		$(function() {	
			
        	$("#gift_dialog").dialog({
				autoOpen: false,
				height: 350,
				width: 550,
				modal: true,
				close: function() {
					
					$('#selector-body').html('');
				}
			});
        });
        function openProductDialog(){		 	
        	searchSubmit(1);
			$('#gift_dialog').dialog('open');
		}
	
	
	
	

		
		
		
		
		
		
		
		
		
		
		
	
		
		
	$(document).ready(function() {
		var validator = $("#mt_activity_rule_form").validate( {
			errorElement : "div",
			errorClass : "x-vali-error",
			onfocusout : false,
			rules : {
 				'marketPromotion.orderMoneyFrom' : {
					required : true,
					number : true
				},
				'marketPromotion.orderMoneyTo' : {
					required : true,
					number : true
				},
				'marketPromotion.pmtTimeBeginString' : "required",
				'marketPromotion.pmtTimeEndString' : "required",
				 
				'marketPromotion.pmtDescribe' : "required" 
			},
			messages : {
			
			
				'marketPromotion.orderMoneyFrom' : {
					required : "必填字段",
					number : "请输入数字"
				},
				'marketPromotion.orderMoneyTo' : {
					required : "必填字段",
					number : "请输入数字"
				},
				'marketPromotion.pmtTimeBeginString' : "必填字段",
				'marketPromotion.pmtTimeEndString' : "必填字段",
				 
				'marketPromotion.pmtDescribe' : "必填字段"
				 
			},
			errorPlacement : function(error, element) {
				element.removeClass('x-vali-error');
				error.appendTo(element.parent());

			},
			 
			success : function(div) { 
				$(div).remove();
			}
		});
	});
	
	
	function checkLevel(){
		var selected = false;
		$('#promotion_level input[vname]').each(function(i, dom) {
			if ($(dom).attr("checked")) {
				selected = true;
			}
		});
		if(!selected){
			alert('请至少选择一个等级');
		}
		return selected;
	}
	
	$("#rule_start_time").datepicker( $.datepicker.regional['zh-CN']);
	$("#rule_end_time").datepicker( $.datepicker.regional['zh-CN']);
</script>
	</body>
</html>